<template>
  <div>
    <a-row :gutter="5">
      <a-col :span="4">
        <a-card title="组织部门列表">
          <a-select
            style="width: 100%"
            @change="orgDimChange"
            placeholder="请选择组织纬度"
          >
            <a-select-option
              v-for="item in orgDims"
              :key="item.id"
              :value="item.id"
              >{{ item.name }}</a-select-option
            >
          </a-select>

          <a-tree
            show-line
            show-icon
            default-expand-all
            :style="{
              maxHeight: '520px',
              minHeight: '550px',
              'overflow-y': 'auto',
            }"
            :tree-data="orgs"
            @select="handleTreeSelect"
          />
        </a-card>
      </a-col>
      <a-col :span="20">
        <!--查询参数-->
        <params
          ref="params"
          @handleRefresh="handleRefresh"
          :loading="loading"
        ></params>
        <!--表格页面-->
        <my-table
          ref="myTable"
          :dataSource="dataSource"
          :columns="columns"
          :loading="loading"
          @handleAdd="handleAdd"
          @removeSubmit="removeSubmit"
          @handleEdit="handleEdit"
          @handleImport="handleImport"
          @handleExport="handleExport"
          @handleDetail="handleDetail"
          @loadData="loadData"
          addAction="position_create"
          deleteAction="position_delete"
          editAction="position_edit"
          detailAction="position_details"
          importAction="position_import"
          exportAction="position_export"
        />
        <!--修改页面-->
        <modify
          v-if="modifyEntity.showModify"
          @handleRefresh="handleRefresh"
          :modifyEntity="modifyEntity"
          :orgDimId="orgDimId"
        />
      </a-col>
    </a-row>
  </div>
</template>
<script>
import modify from "./modify";
import myTable from "_c/myTable/index";
import params from "./components/params";

import { queryOrgDimSelect } from "@/api/sys/identity/orgDim";
import { queryOrgTree } from "@/api/sys/identity/org";
import {
  positionColumns,
  queryPosition,
  deletePosition,
} from "@/api/sys/identity/position";
export default {
  name: "Position",
  components: { params, modify, myTable },
  data() {
    return {
      modifyEntity: {
        // 传递的主键
        id: "",
        // 页面标题
        title: "",
        // 页面操作类型
        operation: "",
        // 是否展示编辑页面
        showModify: false,
      },
      // 加载动画
      loading: false,
      // 绑定的数据源
      dataSource: [],
      // 表格绑定的列
      columns: positionColumns,
      // 组织纬度
      orgDims: [],
      // 组织部门
      orgs: [],
      // 当前选择的组织纬度
      orgDimId: "",
      // 当前选中的组织部门
      orgId: "",
    };
  },
  mounted() {
    this.loadData();
    queryOrgDimSelect().then((res) => {
      this.orgDims = res.data;
    });
  },
  methods: {
    //#region 左侧下拉&树
    orgDimChange(val) {
      if (val) {
        this.orgDimId = val;
        queryOrgTree({ orgDimId: val }).then((res) => {
          this.orgs = res.data;
        });
      }
    },
    // 左侧树点击
    handleTreeSelect(selectedKeys) {
      if (selectedKeys.length > 0) {
        this.orgId = selectedKeys[0];
        this.loadData();
      }
    },
    //#endregion

    //#region 查询列表
    // 保存/修改后刷新表格
    handleRefresh() {
      this.modifyEntity.showModify = false;
      this.$refs.myTable.pagination.current = 1;
      this.loadData();
    },
    // 加载数据
    loadData() {
      this.loading = true;
      this.$refs.params.params.orgId = this.orgId;
      let queryParams = {
        ...this.$refs.params.params,
        current: this.$refs.myTable.pagination.current,
        pageSize: this.$refs.myTable.pagination.pageSize,
      };
      let that = this;
      queryPosition(queryParams)
        .then((res) => {
          let { data, totalCount } = res.data;
          that.dataSource = data;
          this.$refs.myTable.pagination.total = totalCount;
          that.loading = false;
        })
        .catch(() => {
          that.loading = false;
        });
    },
    //#endregion

    //#region 添加
    // 打开添加页面
    handleAdd() {
      this.modifyEntity = {
        title: "新增岗位信息",
        showModify: true,
        operation: this.operationFlag.Add,
      };
    },
    //#endregion

    //#region 删除
    // 删除
    removeSubmit(ids) {
      deletePosition(ids).then((res) => {
        if (res.code==200) {
          this.$notification.success({ description: res.msg });
          this.handleRefresh();
        } else {
          this.$notification.error({ description: res.msg });
        }
      });
    },
    //#endregion

    //#region 修改
    // 打开编辑页面
    handleEdit(item) {
      this.modifyEntity = {
        id: item.id,
        title: "修改岗位信息",
        showModify: true,
        operation: this.operationFlag.Edit,
      };
    },
    //#endregion

    //#region 详情
    // 打开详情页面
    handleDetail(item) {
      this.modifyEntity = {
        id: item.id,
        title: "岗位信息详情",
        showModify: true,
        operation: this.operationFlag.Detail,
      };
    },
    //#endregion

    //#region 导入
    // 导入数据
    handleImport() {},
    //#endregion

    //#region 导出
    // 导出数据
    handleExport() {},
    //#endregion
  },
};
</script>


